package com.xiegang.servicedriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiegang.internalcommon.constant.CommonStatusEnum;
import com.xiegang.internalcommon.constant.DriverCarContants;
import com.xiegang.internalcommon.dto.DriverCarBindingRelationship;
import com.xiegang.internalcommon.dto.ResponseResult;
import com.xiegang.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author xieGang
 * @since 2023-03-30
 */
@Service
public class DriverCarBindingRelationshipService {

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {

        int checkBind = checkBind(driverCarBindingRelationship);
        if (checkBind > 0) {
            switch (checkBind) {
                case 1:
                    return ResponseResult.fail(CommonStatusEnum.DRIVER_EXISTS.getCode(), CommonStatusEnum.DRIVER_EXISTS.getMessage());
                case 2:
                    return ResponseResult.fail(CommonStatusEnum.CAR_EXISTS.getCode(), CommonStatusEnum.CAR_EXISTS.getMessage());
                default:break;
            }
        }


        LocalDateTime now = LocalDateTime.now();
        driverCarBindingRelationship.setBindingTime(now);
        driverCarBindingRelationship.setBindState(DriverCarContants.DRIVER_CAR_BIND);
        driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
        return ResponseResult.success();
    }

    public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship) {
        LocalDateTime now = LocalDateTime.now();

        Map<String,Object> map = new HashMap<>();
        map.put("driver_id", driverCarBindingRelationship.getDriverId());
        map.put("car_id", driverCarBindingRelationship.getCarId());
        map.put("bind_state", DriverCarContants.DRIVER_CAR_BIND);

        List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingRelationshipMapper.selectByMap(map);
        if (driverCarBindingRelationships.isEmpty()) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getMessage());
        }
        DriverCarBindingRelationship relationship = driverCarBindingRelationships.get(0);
        relationship.setUnBindingTime(now);
        relationship.setBindState(DriverCarContants.DRIVER_CAR_UNBIND);
        driverCarBindingRelationshipMapper.updateById(relationship);

        return ResponseResult.success();
    }

    private int checkBind(DriverCarBindingRelationship driverCarBindingRelationship) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        wrapper.eq("bind_state", DriverCarContants.DRIVER_CAR_BIND);
        Integer count = driverCarBindingRelationshipMapper.selectCount(wrapper);
        if (count > 0) {
            return 1;
        }
        wrapper = new QueryWrapper();
        wrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        wrapper.eq("bind_state", DriverCarContants.DRIVER_CAR_BIND);
        count = driverCarBindingRelationshipMapper.selectCount(wrapper);
        if (count > 0) {
            return 2;
        }
        return 0;
    }
}
